package org.csu.mypetstore.api.controller.front;

import org.csu.mypetstore.api.common.CommonResponse;
import org.csu.mypetstore.api.service.CartService;
import org.csu.mypetstore.api.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/cart/")
public class CartController {

    @Autowired
    private CartService cartService;

//    查看购物车
    @GetMapping("cartItems")
    @ResponseBody
    public CommonResponse<CartVO> getCart(HttpSession session){
        return cartService.viewCart(session);
    }

//    添加商品到购物车
    @PutMapping("cartItems/{id}")
    @ResponseBody
    public CommonResponse<CartVO> addItemToCart(@PathVariable("id") String itemId,HttpSession session){
        return cartService.addItemToCart(session,itemId);
    }

//    修改商品数量
    @PostMapping("cartItems/{id}/quantity/{num}")
    @ResponseBody
    public CommonResponse<CartVO> modifyItemQuantity(@PathVariable("id") String itemId,@PathVariable("num") int newQuantity,HttpSession session){
        return cartService.modifyItemQuantity(itemId,newQuantity,session);
    }

//    删除商品
    @DeleteMapping("cartItems/{id}")
    @ResponseBody
    public CommonResponse<CartVO> deleteItemFromCart(@PathVariable("id") String itemId,HttpSession session){
        return cartService.deleteItemFromCart(itemId,session);
    }
}
